package ywh.player
{
	import flash.utils.getTimer;
	
	/**
	 * 显示计时,此类依赖一个真正的计时器触发查询时间，并不会主动地触发查询时间;
	 * @author WilliWHYang
	 */
	public class ShowerTimer implements ITimeMedia, ICourseMedia
	{
		private var _tempStart:Number;
		private var _isPaused:Boolean;
		private var _time:Number;
		private var _duration:Number;
		private var _courseMedia:ICourseMedia;
		
		public function ShowerTimer()
		{
		
		}
		
		/* INTERFACE player.ICourseMedia */
		
		public function set playItem(value:CourseItem):void
		{
			_courseMedia.playItem = value;
		}
		
		public function get playItem():CourseItem
		{
			return _courseMedia.playItem;
		}
		
		public function init():void
		{
			_courseMedia.init();
			_time = 0;
			_duration = _courseMedia.playItem.duration;
		}
		
		public function destroy():void
		{
			_courseMedia.destroy();
			_tempStart = NaN;
			_isPaused = false;
			_time = NaN;
			_duration = NaN;
		
		}
		
		public function get state():String
		{
			return _isPaused ? PlayerState.PAUSED : _courseMedia.state;
		}
		
		/**
		 * 播放
		 */
		public function play():void
		{
			if (_isPaused)
			{
				_tempStart = getTimer();
				_isPaused = false;
			}
		}
		
		/**
		 * 暂停
		 */
		public function pause():void
		{
			if (!_isPaused)
			{
				_time = time;
				_tempStart = NaN;
				_isPaused = true;
			}
		}
		
		/* INTERFACE ywh.player.ICourseMedia */
		
		public function preSyncHandle():Boolean
		{
			return false;
		}
		
		public function syncHandle(trueTime:Number):void
		{
			_time = trueTime;
		}
		
		/**
		 * 获取当前播放的位置;
		 */
		public function get time():Number
		{
			
			if (!_isPaused)
			{
				var ctime:Number = getTimer();
				_time += (ctime - _tempStart);
				_tempStart = ctime;
			}
			return _time;
		}
		
		/**
		 * 设置时间：
		 */
		public function set time(value:Number):void
		{
			_time = value;
		}
		
		/**
		 * 获取时长
		 * @return
		 */
		public function get duration():Number
		{
			return _duration;
		}
	
	}

}